﻿// Copyright 2008 (c) Michael Reichenauer, SWEDEN. All rights reserved.
//---------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;

namespace PSMock.Implementation
{
	internal static class Util
	{

		/// <summary>
		/// Returns the default return value for the function, which returns a value type 
		/// (e.g. integer is 0)
		/// or null for a reference type or for a method, where return type is void.
		/// </summary>
		internal static object DefaultValue(MethodBase methodBase)
		{
			if (methodBase.IsConstructor)
			{
				return null;
			}

			MethodInfo info = (MethodInfo)methodBase;
			if (info.ReturnType.IsValueType && info.ReturnType.Name != "Void")
			{
				return Activator.CreateInstance(info.ReturnType);
			}
			else
			{
				// Return type void
				return null;
			}
		}


		/// <summary>
		/// Returns the type of class declaring the method
		/// </summary>
		internal static Type DeclaringType(MethodBase methodBase)
		{
			if (methodBase.IsConstructor)
			{
				return (methodBase as ConstructorInfo).DeclaringType;
			}
			else
			{
				return (methodBase as MemberInfo).DeclaringType;
			}
		}
	}
}
